﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace PyroGen
{
    public class Property
    {
        public String Value;
        public String Type;
        public String Name;

        public Property(String name, String type, String value)
        {
            Name = name;
            Value = value;
            Type = type;
        }


        public Property(String name, String type)
        {
            Name = name;
            Value = null;
            Type = type;
        }

        public bool IsTyped()
        {
            if (Type != null && Name != null)
                return true;
            else
                return false;
        }
    }

    public class ConfigHandler
    {
        private List<String> property;
        private List<String> value;

        public ConfigHandler()
        {
            property = new List<string>();
            value = new List<string>();
        }

        public ConfigHandler(String text)
        {
            property = new List<string>();
            value = new List<string>();
            AddConfigString(text);
        }


        public void AddConfigString(String val)
        {
            // TODO: implement checkFormat();
            // TODO: regex possibly
            // TODO: error handling and output
            // TODO: empty values
            string[] tokens = val.Split(new char[] { '=', '\"', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length % 2 != 0)
            {
                Debug.Write("Config string malformed");
                return;
            }
            for (int i = 0; i < tokens.Length; i++)
                if (i % 2 == 0)
                    property.Add(tokens[i]);
                else
                    value.Add(tokens[i]);
        }

        public List<string> FindValuesOfConfig(string key)
        {
            List<String> ret = new List<string>();
            for (int i = 0; i < property.Count; i++)
                if (property[i].Equals(key))
                    ret.Add(value[i]);
            return ret;
        }

        public String FindValueOfConfig(string key)
        {
            List<string> vals = FindValuesOfConfig(key);
            if (vals.Count > 1 || vals.Count == 0)
                return null;
            else
                return vals[0];
        }

        public bool IsProperty()
        {
            if (FindValuesOfConfig(SettingsLoader.GetPropertyKeyword()).Count > 0)
                return true;
            else
                return false;
        }

        public Property GetProperty()
        {
            if (IsProperty())
            {

                // TODO: Malformed or repeated type? Missing value? Better handling necesary
                string name = FindValueOfConfig(SettingsLoader.GetPropertyKeyword());
                string type = FindValueOfConfig(SettingsLoader.GetPropertyTypeKeyword());
                string value = FindValueOfConfig(SettingsLoader.GetPropertyValueKeyword());
                return new Property(name, type);
            }
            else
            {
                return null;
            }
        }


        public List<String> GetNamespaces()
        {
            string namespaceKeyword = SettingsLoader.GetNamespaceKeyword();
            return FindValuesOfConfig(namespaceKeyword);
        }

        public List<String> GetAssemblies()
        {
            string assembliesKeyword = SettingsLoader.GetAssemblyKeyword();
            return FindValuesOfConfig(assembliesKeyword);
        }

        public List<String> GetIncludes()
        {
            string includeKeyword = SettingsLoader.GetIncludeKeyword();
            return FindValuesOfConfig(includeKeyword);
        }

    }
}
